package cn.action;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import cn.biz.BrandBiz;
import cn.biz.ConfigurationBiz;
import cn.biz.GoodsBiz;
import cn.entity.Brand;
import cn.entity.Configuration;
import cn.entity.Goods;
import cn.entity.User;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;

public class GoodsAction implements Action {
	private GoodsBiz goodsBiz;
	private BrandBiz brandBiz;
	private ConfigurationBiz configurationBiz;
	private List<Goods> Glist = new ArrayList<Goods>();
	private Goods goods;
	private String resultStr = "";
	private Goods detail;
	private String searchKey;
	private List<Brand> Blist;
	private List<Configuration> typeList;

	public Map<String, Object> getSession() {
		return ActionContext.getContext().getSession();
	}

	public User getSessionUser() {
		return (User) getSession().get("loginUser");
	}

	public GoodsBiz getGoodsBiz() {
		return goodsBiz;
	}

	public void setGoodsBiz(GoodsBiz goodsBiz) {
		this.goodsBiz = goodsBiz;
	}

	public BrandBiz getBrandBiz() {
		return brandBiz;
	}

	public void setBrandBiz(BrandBiz brandBiz) {
		this.brandBiz = brandBiz;
	}

	public ConfigurationBiz getConfigurationBiz() {
		return configurationBiz;
	}

	public void setConfigurationBiz(ConfigurationBiz configurationBiz) {
		this.configurationBiz = configurationBiz;
	}

	public List<Goods> getGlist() {
		return Glist;
	}

	public void setGlist(List<Goods> glist) {
		Glist = glist;
	}

	public Goods getGoods() {
		return goods;
	}

	public void setGoods(Goods goods) {
		this.goods = goods;
	}

	public String getResultStr() {
		return resultStr;
	}

	public void setResultStr(String resultStr) {
		this.resultStr = resultStr;
	}

	public Goods getDetail() {
		return detail;
	}

	public void setDetail(Goods detail) {
		this.detail = detail;
	}

	public String getSearchKey() {
		return searchKey;
	}

	public void setSearchKey(String searchKey) {
		this.searchKey = searchKey;
	}

	public List<Brand> getBlist() {
		return Blist;
	}

	public void setBlist(List<Brand> blist) {
		Blist = blist;
	}

	public List<Configuration> getTypeList() {
		return typeList;
	}

	public void setTypeList(List<Configuration> typeList) {
		this.typeList = typeList;
	}

	/*
	 * 增加一条商品信息
	 */
	public String insertOrUpdateGoods() {
		if(null == goods.getUser()){
			goods.setUser(getSessionUser());
		}
		return goodsBiz.insertOrUpdate(goods);
	}

	/*
	 * 删除一条商品信息
	 */
	public String delete() {
		//goods.setId(Gid);
		//return goodsBiz.delete(goods);
		return null;
	}

	/*
	 * 跳转至商品信息
	 */
	public String goodDetail() {
		ActionContext context = ActionContext.getContext();
		HttpServletRequest request = ServletActionContext.getRequest();
		String id = request.getParameter("gid");
		//Gid = new Integer(id);
		// System.out.println("GGGGID  " + Gid);
		// System.out.println("gid: " + id);
		// List good = goodsBiz.getGoodById(Integer.valueOf(id));
		detail = goodsBiz.getGoodsById(Integer.valueOf(id));
		return SUCCESS;
	}

	public String query() {
		if(StringUtils.isBlank(searchKey)||"请输入关键字".equals(searchKey)){
			Glist = goodsBiz.getAll();
		} else {
			Goods goods = new Goods();
			goods.setGoodsname(searchKey);
			/*调用goods的模糊查询 在goodsname中查询关键字*/
			Glist = goodsBiz.select(goods);
		}
		StringBuffer str = new StringBuffer("{result:[");
		if (Glist.size() > 0) {
			int j = 0;
			for (int i = 0; i < Glist.size(); i++) {
				if (j > 0) {
					str.append(",");
				}
				Goods g = (Goods) Glist.get(i);
				str.append("{");
				str.append("id:'").append(g.getId()).append("',");
				String img = g.getImg();
				if(StringUtils.isBlank(img)) {
					img = "images/pic1.jpg";
				}
				str.append("img:'").append(img).append("',");
				str.append("goodsname:'").append(g.getGoodsname()).append("',");
				str.append("remark:'");
				if(g.getRemark().length()>20){
					str.append(g.getRemark().substring(0, 20) + "...");
				} else {
					str.append(g.getRemark());
				}
						
				str.append("',");
				str.append("price:").append(g.getPrice());
				str.append("}");
				j++;
			}

		}
		str.append("]}");
		// Map<String, Object> map = new HashMap<String, Object>();
		// map.put("result", Glist);
		resultStr = "(" + str.toString() + ")";// 给result赋值，传递给页面

		return SUCCESS;
	}

	/*
	 * 模糊查询
	 */
	public String select() {
		Glist = goodsBiz.select(goods);
		return SUCCESS;
	}

	/*
	 * 卖家登陆时获取商品信息
	 */
	public String getGoodsBySeller() {
		Glist = goodsBiz.getGoodsBySeller(getSessionUser().getId());
		return SUCCESS;
	}
	
	/*
	 * 根据前台操作改变goods的删除状态
	 */
	public String changeGoodsDelete(){
		try {
			Integer isDelete = goods.getIsDelete();
			Integer id = goods.getId();
			Goods goods = goodsBiz.getGoodsById(id);
			goods.setIsDelete(isDelete);
			goodsBiz.update(goods);
			resultStr = "{\"flag\":\"success\"}";
		} catch (Exception e) {
			resultStr = "{\"flag\":\"error\"}";
		}
		return SUCCESS;
	}
	
	public String viewGoods() {
		goods = goodsBiz.getGoodsById(goods.getId());
		Blist = brandBiz.getAll();
		typeList = configurationBiz.getStatusByStatusType("硬盘类型");
		return SUCCESS;
	}
	
	public String addGoods() {
		goods = new Goods();
		Blist = brandBiz.getAll();
		typeList = configurationBiz.getStatusByStatusType("硬盘类型");
		return SUCCESS;
	}
	
	public String goodsShow() {
		Glist = goodsBiz.getManage();
		return SUCCESS;
	}
	
	@Override
	public String execute() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

}
